<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">蓝湖房产后台管理系统</h3>
      </div>

      <el-form-item style="background: rgba(255,255,255,.9)" prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </el-form-item>

      <el-form-item style="background: rgba(255,255,255,.9)" prop="email">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="email"
          v-model="loginForm.email"
          placeholder="电子邮箱"
          type="text"
          name="email"
          tabindex="2"
          auto-complete="off"
        />
      </el-form-item>

      <el-form-item style="background: rgba(255,255,255,.9)" prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="确认密码"
          name="password"
          tabindex="2"
          auto-complete="off"
          @keyup.enter.native="handRegister"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item style="background: rgba(255,255,255,.9)" prop="confirmPwd">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="confirmPwd"
          v-model="loginForm.confirmPwd"
          :type="passwordType"
          placeholder="再次确认密码"
          name="confirmPwd"
          tabindex="2"
          auto-complete="off"
          @keyup.enter.native="handRegister"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handRegister">注册</el-button>

      <div class="LoginBtn" :loading="loading" type="button" style="width:100%;margin-bottom:30px;color:#fff;cursor:pointer" @click="handleLogin">去登录</div>

    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
// import { mapMutations } from 'vuex'
// 引入api接口
import { register } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('该项为必填!!'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('该项为必填!!'))
      } else {
        callback()
      }
      if (value.length < 6 && value.length > 0) {
        callback(new Error('长度在 6 到 16 个字符!!'))
      } else {
        callback()
      }
    }
    const validateConfirmPwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('该项为必填!!'))
      } else {
        callback()
      }
      if (value.length < 6 && value.length > 0) {
        callback(new Error('长度在 6 到 16 个字符!!'))
      } else {
        callback()
      }
    }

    const validateEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('该项为必填!!'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: '',
        email: '',
        password: '',
        confirmPwd: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],

        password: [
          { required: true, trigger: 'blur', message: '该项为必填', validator: validatePassword },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, trigger: 'blur', message: '该项为必填', validator: validateConfirmPwd },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        // 邮箱校验
        email: [
          { required: true, trigger: 'blur', message: '该项为必填', validator: validateEmail },
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },

  methods: {
    // 用辅助函数展开token
    // ...mapMutations('user', ['amendToken', 'getUserId', 'getRegister']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录事件
    handleLogin() {
      this.$router.push('/login')
    },
    // 注册事件/
    async handRegister() {
      // 校验表单
      await this.$refs.loginForm.validate()
      const { username, email, password } = this.loginForm
      // 判断两次密码是不是一致
      if (password === this.loginForm.confirmPwd) {
        try {
          //   发送请求
          const res = await register({ username, email, password })
          console.log(198, res)
          // 判断返回的结果
          if (res.status === 200 && res.statusText === 'OK') {
            this.$message({
              message: '注册成功',
              type: 'success'
            })
            //   成功之后就跳转
            this.$router.push('/login')
          }
        } catch (error) {
          console.dir(206, error)
          console.log(200)

          // console.dir(206, error.message[0].message[0].message[0].message)
        }
      } else {
        this.$message.error('两次输入密码不对')
      }
    }
  }
}
</script>

<style lang="scss">
// 注册样式
.LoginBtn{
margin-left: 85%;
color: green;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
       color: black;
      caret-color: balck;

      &:-webkit-autofill {
          transition-delay: 99999s;
        transition: color 99999s ease-out, background-color 99999s ease-out;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('~@/assets/image/bj5.jpg')  no-repeat   ;
  background-position: 50% 70%;
  // background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
